package com.zero.manage.web.components.controller;

import com.zero.manage.web.components.base.BaseController;
import com.zero.manage.web.components.dto.GenDto;
import com.zero.manage.web.components.entity.DatabaseInfoEntity;
import com.zero.manage.web.components.query.DatabaseInfoQuery;
import com.zero.manage.web.components.service.DatabaseInfoService;
import io.gitee.zerowsh.util.PublicResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;

@RestController
@Api(tags = "数据库管理")
@RequestMapping("/databaseInfo")
public class DatabaseInfoController extends BaseController<DatabaseInfoEntity, String, DatabaseInfoService, DatabaseInfoQuery> {
    @Autowired
    private DatabaseInfoService databaseInfoService;

    @GetMapping("select")
    @ApiOperation(value = "数据库管理下拉框", notes = "数据库管理编号和名称下拉框")
    public PublicResult select() {
        return databaseInfoService.select();
    }

    @GetMapping("getCatalogs")
    @ApiOperation(value = "连接下所有数据库", notes = "获取连接下所有数据库名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "databaseInfoId", value = "数据库管理逐渐", required = true)
    })
    public PublicResult getCatalogs(@RequestParam String databaseInfoId) {
        return databaseInfoService.getCatalogs(databaseInfoId);
    }

    @GetMapping("getTableInfo")
    @ApiOperation(value = "指定数据库下所有表", notes = "获取指定数据库下所有表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "databaseInfoId", value = "数据库管理逐渐", required = true),
            @ApiImplicitParam(name = "catalog", value = "数据库名称", required = true)
    })
    public PublicResult getTableInfoByCatalog(@RequestParam String databaseInfoId,
                                              @RequestParam String catalog) {
        return databaseInfoService.getTableInfo(databaseInfoId, catalog);
    }

    @PostMapping("generatorCode")
    @ApiOperation(value = "代码生成", notes = "通过数据库相关信息生成代码文件或者zip压缩包")
    public PublicResult generatorCode(@RequestBody @Valid GenDto genDto, HttpServletResponse response) throws IOException {
        return databaseInfoService.generatorCode(genDto, response);
    }

}
